/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.designer.impl;

import org.soupframework.gwt.designerpanel.client.command.Command;
import org.soupframework.gwt.designerpanel.client.designer.ContainerDesigner;
import org.soupframework.gwt.designerpanel.client.designer.DesignerManager;
import org.soupframework.gwt.designerpanel.client.dragproxy.DragProxy;

import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public abstract class HorizontalPanelViewDesigner<T> extends CellPanelViewDesigner<T> {

	public HorizontalPanelViewDesigner(DesignerManager<T> manager, ContainerDesigner<T> parent, T object) {
		super(manager, parent, object);
	}

	public class Operations extends CellPanelViewDesigner<T>.Operations {
		@Override
		public void addObject(T object, int left, int top) {
			int beforeIndex = getBeforeIndex(left, top);
			if(beforeIndex >= 0){
				getObjectView().insert(getChildObjectView(object), beforeIndex);
				super.addObject(object, left, top, beforeIndex);
			}
			else{
				getObjectView().add(getChildObjectView(object));
				super.addObject(object, left, top);
			}
		}
	}

	@Override
	public Operations getOperations(){
		if(this.operations != null){
			return (Operations)this.operations;
		}
		this.operations = this.new Operations();
		return (Operations)this.operations;
	}

	private int getBeforeIndex(int left, int top){
		int x = left + getObjectView().getAbsoluteLeft();
		int beforeIndex = -1;
		for(int i = 0 ; i < getObjectView().getWidgetCount() ; i ++){
			Widget w = getObjectView().getWidget(i);
			int ox = w.getAbsoluteLeft() + w.getOffsetWidth();
			if(ox >= x){
				beforeIndex = i;
				break;
			}
		}
		return beforeIndex;
	}

	/**
	 * Exchange the given objects. It differs from the exchangeObjects method in that subsequent calls to exchangeObjectsPartial
	 * cumulate to the same command group in history
	 */
	public void exchangeObjectsPartial(String commandGroupTypePrefix, T object1, T object2){
		Command command = this.getManager().getCommandFactory().createHorizontalExchangeCommand(this, object1, object2);
		this.getManager().doPartialCommand("CommandGroupTypeHorizontalExchange-" + commandGroupTypePrefix, command);
	}

	@Override
	protected void showDragProxyInt(DragProxy<T> dragProxy, int left, int top){
		int beforeIndex = getBeforeIndex(left, top);
		if(beforeIndex >= 0){
			getObjectView().insert(dragProxy.getProxyWidget(), beforeIndex);
		}
		else{
			getObjectView().add(dragProxy.getProxyWidget());
		}
	}

	@Override
	protected void hideDragProxyInt(DragProxy<T> dragProxy){
		getObjectView().remove(dragProxy.getProxyWidget());
	}

	@Override
	public abstract HorizontalPanel getObjectView();
}
